<template>
  <f7-page
    :name="name"
    @page:init="pageInit"
    @page:afterin="onPageAfterIn"
    @page:beforeout="onPageBeforeOut"
  >
    <f7-navbar style="background-color: #fff;">
      <f7-nav-title style="width: 100%;height: 100%;margin-left: 0px;">
        <f7-list no-hairlines-md>
          <li>
            <div class="item-content item-input">
              <div class="item-inner">
                <div class="item-input-wrap search-input-frame no-after">
                  <input
                    type="text"
                    name="keywords"
                    :placeholder="placeholderInfo"
                    v-model.trim="keyword"
                    @input="onUpdateSearch"
                  />
                </div>
              </div>
            </div>
          </li>
        </f7-list>
      </f7-nav-title>
    </f7-navbar>
    <div class="scroll-fixed-box">
      <div class="search-result-list">
        <!-- 办事流程 -->
        <div class="list" v-if="type == sourceType.workflow">
          <ul>
            <li v-for="el in searchList" :key="el.id" @click="tipSearchDetailPage(el)">
              <a href="#" class="item-link item-content">
                <div class="item-inner">
                  <div class="item-title" v-text="el.title"></div>
                  <div class="item-after" v-text="el.create_time"></div>
                </div>
              </a>
            </li>
          </ul>
        </div>
        <!-- 政策法规 -->
        <div class="list" v-if="type == sourceType.law">
          <ul>
            <li v-for="el in searchList" :key="el.id" @click="tipSearchDetailPage(el)">
              <a href="#" class="item-link item-content">
                <div class="item-inner">
                  <div class="item-title" v-text="el.title"></div>
                  <div class="item-after" v-text="el.create_time"></div>
                </div>
              </a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </f7-page>
</template>

<script>
import Log from "../../mod/log.js";
import strings from "../../assets/strings";
import cordova from "../../js/cordova-app";

import Utils from "../../js/mod.utils";
import Http from "../../js/mod.http";
import Storage from "../../mod/storage";
import StringUtils from "../../js/mod.string";
import ConstantUtils from "../../js/vue.constant";

const name = strings.page.search;
const log = new Log(name);

export default {
  components: {},
  data() {
    return {
      name: name,
      documentTitle: "搜索",
      placeholderInfo: "搜索",
      from: "",
      type: "",
      id: 0,

      loading: true,
      preloader: true,
      pageSize: 10,
      curPage: 1,
      totalPage: 1,

      keyword: "",
      searchList: [],

      sourceType: {
        workflow: ConstantUtils.type.workflow,
        law: ConstantUtils.type.law
      }
    };
  },
  methods: {
    pageInit(event, pageData) {
      let _this = this;
      let query = Utils.getParamData(_this);
      _this.type = query.type ? query.type : "";

      _this.getSearchListBy(_this.curPage);
      // 弹出输入法
      _this.$nextTick(function() {
        _this.$$(".search-input-frame #search_input_id").focus();
      });
    },
    onPageAfterIn() {
      let _this = this;
      Utils.setTitle(_this, "搜索");
      Utils.setBack(_this, function(e) {
        _this.onBack();
      });
      // cordova.addAndroidHook(_this);
    },
    onPageBeforeOut() {
      let _this = this;
      // cordova.removeAndroidHook(_this);
    },
    onUpdateSearch: function() {
      let _this = this;
      _this.getSearchListBy(1);
    },
    getNextSearchList: function() {
      let _this = this;
      if (!_this.loading) {
        return;
      }
      _this.loading = false;
      setTimeout(function() {
        if (_this.curPage >= _this.totalPage) {
          _this.preloader = false;
          return;
        }
        let curPageNumber = _this.curPage + 1;
        _this.getSearchListBy(curPageNumber);
        _this.loading = true;
      }, 1000);
    },
    getSearchListBy: function(curPage) {
      let _this = this;
      if (_this.type == "") {
        return false;
      }
      if (_this.keyword == "") {
        _this.searchList = [];
        return false;
      }
      var search = [];
      if (_this.keyword) {
        search.push({
          keyWord: _this.keyword,
          logical: "and"
        });
      }
      let data = {
        page: curPage,
        size: _this.pageSize,
        search: search
      };
      let to_url = "";
      let warnInfo = "";
      switch (_this.type) {
        case ConstantUtils.type.workflow:
          to_url = Http.kInterfacePath.workflow.list;
          warnInfo = "获取第" + curPage + "页的办事流程列表失败！";
          break;
        case ConstantUtils.type.law:
          to_url = Http.kInterfacePath.law.list;
          warnInfo = "获取第" + curPage + "页的法律法规列表失败！";
          break;
        default:
          break;
      }
      if (to_url == "") {
        return false;
      }
      let params = {
        url: to_url,
        data: data,
        warn_error: warnInfo,
        success: _successList
      };
      Http.ajax(_this, params);

      function _successList(res) {
        _this.totalPage = res.total ? res.total : 1;
        _this.curPage = res.page;
        let curResData = res.data ? res.data : [];
        if (_this.totalPage == 1) {
          _this.preloader = false;
        }

        if (_this.curPage <= 1) {
          _this.searchList = curResData;
        } else {
          let afterArr = _this.searchList;
          for (let i in curResData) {
            afterArr.push(curResData[i]);
          }
          _this.searchList = afterArr;
        }
      }
    },
    tipSearchDetailPage: function(item) {
      let _this = this;
      let data = {
        id: item.id ? item.id : -1
      };
      switch (_this.type) {
        case ConstantUtils.type.workflow:
          Utils.load(_this, "/workflow-detail/", data, {});
          break;
        case ConstantUtils.type.law:
          Utils.load(_this, "/law-detail/", data, {});
          break;
        default:
          break;
      }
    },

    onBack() {
      let _this = this;
      _this.$f7router.back();
    },
    onResume() {
      log.w("onResume");
    },
    onPause() {
      log.w("onPause");
    }
  }
};
</script>

<style scoped>
.item-input-wrap.search-input-frame:after {
  height: 0px;
}
</style>
